package com.yiguantong.police.qrCode.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yiguantong.police.R;
import com.yiguantong.police.BaseActivity;
import com.yiguantong.police.qrCode.BisDealUtil;
import com.yiguantong.police.toolbar.ToolbarMenuEnum;
import com.yiguantong.police.webview.WebViewActivity;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author szg 二维码详情
 * 
 */
public class CodeTwoMessage extends BaseActivity implements OnClickListener {
    /**
     * 协带信息
     */
    private String message;

    /**
     * btn按钮
     */
    private Button code2Btn;
    
    private LinearLayout llcode2Tv;

    /**
     * 是否是网址
     */
    private boolean isUrl;

    /**
     * 二维码TextView
     */
    private TextView code2Tv;

    private static final int QRCODE_REQ_CODE = 0X006;

    private String qrcodeUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.code2meaage);
        initToolbar(getResources().getString(R.string.code2_title), ToolbarMenuEnum.PopupIcon);
        code2Btn = (Button) this.findViewById(R.id.code2_btn);
        code2Tv = (TextView) this.findViewById(R.id.code2_tv);
        llcode2Tv = (LinearLayout) this.findViewById(R.id.ll_code2_tv);

        if (null != getIntent()) {
            Bundle bundle = getIntent().getExtras();
            message = bundle.getString("content");
            HashMap<String, Object> hashMap = getUrl(message);
            isUrl = (Boolean) hashMap.get("isUrl");
            String message01 = (String) hashMap.get("message");

            if (isUrl) {
                llcode2Tv.setVisibility(View.GONE);
                code2Btn.setVisibility(View.GONE);
                code2Tv.setText(message01);
                code2Btn.setText("访问网址");
                dealMessage();
            } else {
                llcode2Tv.setVisibility(View.VISIBLE);
                code2Btn.setVisibility(View.VISIBLE);
                code2Tv.setText(message01);
                code2Btn.setText("复制文本");
            }

            code2Btn.setOnClickListener(this);
        }
    }

    private HashMap<String, Object> getUrl(String message) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        boolean b = false;

        String meassage01 = "";

        String httpStr = getHTTP(message);

        if (null != httpStr && !"".equals(httpStr)) {
            meassage01 = httpStr;
            b = true;
        } else {
            String httpsStr = getHTTPS(message);
            if (null != httpsStr && !"".equals(httpsStr)) {
                meassage01 = httpsStr;
                b = true;
            } else {
                meassage01 = message;
                b = false;
            }
        }
        hashMap.put("isUrl", b);
        hashMap.put("message", meassage01);
        return hashMap;

    }

    /**
     * 遍历字符串中包含http的网址
     * 
     * @param message
     * @return
     */
    public static String getHTTP(String message) {
        Pattern pattern = Pattern
                        .compile("http://[\\S\\.]+[:\\d]?[/\\S]+\\??[\\S=\\S&?]+[^\u4e00-\u9fa5]");
        Matcher matcher = pattern.matcher(message);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            buffer.append(matcher.group());
            buffer.append("\r\n");
        }
        return buffer.toString();
    }

    /**
     * 遍历字符串中包含https的网址
     * 
     * @param message
     * @return
     */
    public static String getHTTPS(String message) {
        Pattern pattern = Pattern
                        .compile("https://[\\S\\.]+[:\\d]?[/\\S]+\\??[\\S=\\S&?]+[^\u4e00-\u9fa5]");
        Matcher matcher = pattern.matcher(message);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            buffer.append(matcher.group());
            buffer.append("\r\n");
        }
        return buffer.toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.code2_btn:
            dealMessage();
            break;

        default:
            break;
        }
    }

    @SuppressWarnings("deprecation")
    private void dealMessage() {

        if (isUrl) {
            String httpUrl = code2Tv.getText().toString();
            if (!TextUtils.isEmpty(httpUrl)) {

                String name = BisDealUtil.obtainValueName(httpUrl);
                String initUrl = BisDealUtil.obtainUrlClsName(httpUrl);

                boolean isFlag = false;

                if (TextUtils.isEmpty(name) && (name.trim().equals("activity&qrcode")||name.trim().equals("charge&qrcode"))) {
                    httpUrl = initUrl;
                    if (TextUtils.isEmpty(httpUrl)) {
                        return;
                    }
                    isFlag = true;
                        Intent it = new Intent(CodeTwoMessage.this, WebViewActivity.class);
                        it.putExtra("url", httpUrl);
                        startActivity(it);
                        this.finish();

                }

                if (!isFlag) {
                    Intent localIntent = new Intent();
                    localIntent.setAction("android.intent.action.VIEW");
                    localIntent.setData(Uri.parse(httpUrl));
                    CodeTwoMessage.this.startActivity(localIntent);
                }
            }
        } else {
            String content = code2Tv.getText().toString();

            if (android.os.Build.VERSION.SDK_INT >= 11) {
                android.content.ClipboardManager newcmb = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                newcmb.setText(content);
            } else {
                android.text.ClipboardManager oldcmb = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                oldcmb.setText(content);
            }
            toast("文本已复制");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitCodeTwoMsg();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 方法名称：exitCodeTwoMsg
     * <p>
     * 方法描述：退出二维码结果界面
     * 
     * @author wangZhongfu
     *         <p>
     *         备注：
     */
    private void exitCodeTwoMsg() {
        CodeTwoMessage.this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

}
